<template>
  <div class="infoImportManagement-container">
    <el-row class="header-btn">
      <el-col :span="12" class="editAdd">
        <el-button icon="el-icon-plus" type="primary" size="small" @click="addDialogVisible = true">
          添加用户
        </el-button>
        <el-button icon="el-icon-delete" type="danger" size="small">
          批量删除
        </el-button>
      </el-col>
      <el-col :span="12" class="query">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
                size="small"
                v-model.trim="queryForm.query"
                placeholder="请输入查询条件"
                clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" size="small" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table
        :data="userList"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="userName" label="用户名"></el-table-column>
      <el-table-column prop="loginName" sortable label="登录名"></el-table-column>
      <el-table-column prop="authorityRemark" label="角色" width="200"></el-table-column>
      <el-table-column prop="lastLoginTime" label="最后登录时间" width="230"></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" content="重置密码：123456" placement="top"
                      :enterable="false">
            <el-button size="small" icon="el-icon-unlock" type="info" circle @click="resetPassword(scope.row.userId)"></el-button>
          </el-tooltip>
          <el-button size="small" icon="el-icon-delete" type="danger" circle @click="handleDelete(scope.row.userId)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加用户的对话框 -->
    <el-dialog
        title="添加用户"
        :visible.sync="addDialogVisible"
        width="30%"
        @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addUserForm" :rules="addUserFormRules" ref="addUserRuleFormRef" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="addUserForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="addUserForm.loginName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addUserForm.password"></el-input>
        </el-form-item>
        <el-form-item prop="authorityId" label="选择角色">
          <el-select placeholder="请选择" v-model="addUserForm.authorityId">
            <el-option
                v-for="item in authorityList"
                :key="item.authorityId"
                :label="item.authorityRemark"
                :value="item.authorityId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "index",
  data() {
    return {
      userList: [],
      authorityList: [],
      listLoading: true,
      queryForm: {
        query: '',
      },
      //控制添加对话框的显示与隐藏
      addDialogVisible: false,
      addUserForm: {
        userName: '',
        loginName: '',
        password: '',
        authorityId: ''
      },
      // 添加用户表单的验证规则对象
      addUserFormRules: {
        userName: [
          {required: true, message: '用户名不可为空', trigger: 'blur'}
        ],
        loginName: [
          {required: true, message: '用户登录名不可为空', trigger: 'blur'}
        ],
        authorityId: [
          {required: true, message: '权限不可为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不可为空', trigger: 'blur'},
          {min: 6, max: 18, trigger: 'blur'}
        ]
      },
      editDialogVisible: false,
      editForm: {},
      // 修改表单的验证规则对象
      editRules: {
        userName: [
          {required: true, message: '用户名不可为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.queryData()
    this.authority()
  },
  methods: {

    handleEdit() {

    },
    // 根据用户id删除用户
    async handleDelete(userId) {
      // 提示消息
      const re = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch(err => err)

      // console.log(re)
      if (re != 'confirm') return this.$message.info('取消了删除')

      const {data : res} = await this.$http.get('deleteUser?userId=' + userId)
      if (res.code != 200) return this.$message.error('删除失败')

      this.$message.success('删除成功')
      this.queryData()
    },
    async queryData() {
      const {data : res} = await this.$http.get('users')
      // console.log(res)
      this.userList = res.data
    },
    async authority() {
      const {data : res} = await this.$http.get('authority')
      // console.log(res)
      this.authorityList = res.data
    },
    // 监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$refs.addUserRuleFormRef.resetFields()
    },
    // 添加用户
    addUser() {
      this.$refs.addUserRuleFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加用户的网络请求
        const {data: res} = await this.$http.post('addUser',this.addUserForm)
        // console.log(res)
        if (res.code != 200 || res.data == 0)
          return this.$message.error('用户添加失败，登录名重复！')
        this.$message.success('用户添加成功！')
        this.addDialogVisible = false
        this.queryData()
      })
    },
    // 重置密码
    async resetPassword(userId) {
      // 提示消息
      const re = await this.$confirm('是否确定重置密码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch(err => err)

      // console.log(re)
      if (re != 'confirm') return this.$message.info('取消了操作')
      const {data: res} = await this.$http.get('reset?userId='+ userId)
      if (res.code == 200) return this.$message.success('重置密码成功')
    },


  }
}
</script>

<style lang="less" scoped>
.infoImportManagement-container {
  width: 98%;
  background: white;
  padding: 12px 16px 12px 16px;
}

#tip {
  width: 80%;
  background: white;
  margin-bottom: 6px;
  color: #E6A23C;
}
.header-btn {
  //text-align: left;
  //display: flex;
  //justify-content: space-between;
  //align-items: center;
  //color: black;
  //font-size: 20px;

  >.editAdd {
    height: 42px;
    display: flex;
    align-items: center;
    justify-items: center;
  }

  .query {
    height: 22px;
    margin-top: 22px;
    display: flex;
    align-content: center;
    align-items: center;
    justify-content: flex-end;
  }
}
.el-table >.el-table-column {
  height: 12px;
}

</style>
